﻿using Main.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main
{
    public class WordTestManager
    {


        private static List<WordTestInfo> wordTests = new List<WordTestInfo>();

        static WordTestManager()
        {
            if (File.Exists(SystemConfig.WordTestFilePath))
            {
                string[] strs = File.ReadAllLines(SystemConfig.WordTestFilePath);
                foreach (var str in strs)
                {
                    WordTestInfo wordTestInfo = JsonConvert.DeserializeObject<WordTestInfo>(str);
                    wordTests.Add(wordTestInfo);
                }
            }
        }
        public static void AddNewWordTest(WordTestInfo wordTestInfo)
        {
            string str1 = JsonConvert.SerializeObject(wordTestInfo);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(SystemConfig.WordTestFilePath, true))
            {
                file.WriteLine(str1);
            }
            wordTests.Add(wordTestInfo);
        }

        public static List<WordTestInfo> GetWordTests()
        {
            //排序
            wordTests.Sort(delegate(WordTestInfo a, WordTestInfo b) { return a.Date.CompareTo(b.Date); });
            return wordTests;
        }

        public static void DelWordTest(WordTestInfo wordTestInfo)
        {
            wordTests.Remove(wordTestInfo);
            //重新写入
            string str1 = JsonConvert.SerializeObject(wordTests);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(SystemConfig.WordTestFilePath, false))
            {
                file.WriteLine(str1);
            }
        }
    }
}
